package com.example.demo.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocalResolve implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        // 解析请求  a th:href="@{/index.html(l='zh_CN')}"
        String language = request.getParameter("l");
        // 获取默认
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(language)) {
            // 设置语言
            String[] split = language.split("_");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
